package com.zhida.smartcontrol.pagectrl.lampmanager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.jay.tools.base.BaseFragment;
import com.jay.tools.utils.EventBusUtils;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.mvp.MVPBaseFragment;
import com.zhida.smartcontrol.pagectrl.lampmanager.doublelamp.DoubleLampFragment;
import com.zhida.smartcontrol.pagectrl.lampmanager.editlamp.EditLampFragment;
import com.zhida.smartcontrol.pagectrl.lampmanager.singlelamp.SingleLampFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindViews;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * author: tanjianpei
 * date: 2019/10/17 017 14:47
 * description: 单/双灯控制器
 */
public class LampManagerFragment extends MVPBaseFragment<LampManagerContract.View, LampManagerPresenter> implements LampManagerContract.View {
    @BindViews({R.id.tv_qy, R.id.tv_gz})
    List<TextView> mTvTitles;
    @BindViews({R.id.view_qy, R.id.view_gz})
    List<View> mViews;

    private LampBean mBean;
    private List<BaseFragment> mFragmentList;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mHandler.postDelayed(refreshDevice, 5000);
                    break;
                case 1:
                    mHandler.removeCallbacks(refreshDevice);
                    break;
                case 2:
                    mHandler.post(refreshDevice);
                    break;
            }
        }
    };

    private Runnable refreshDevice = new Runnable() {
        @Override
        public void run() {
            if (null != mBean)
                mPresenter.requestList(mBean.getId(), false);
        }
    };

    public static LampManagerFragment newInstance(LampBean bean) {
        Bundle args = new Bundle();
        args.putParcelable("bean", bean);
        LampManagerFragment fragment = new LampManagerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.fragment_lamp_manager;
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        clickTabDouble(false);

        mBean = getArguments().getParcelable("bean");

        mFragmentList = new ArrayList<>();
        mFragmentList.add(SingleLampFragment.newInstance(mBean));
        mFragmentList.add(DoubleLampFragment.newInstance(mBean));

        final SupportFragment[] fragmentArray = mFragmentList.toArray(new SupportFragment[mFragmentList.size()]);
        loadMultipleRootFragment(R.id.fl_container, 0, fragmentArray);

        mPresenter.requestList(mBean.getId(), true);

        EventBusUtils.register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshEdit(EditLampOkEvent bean) {
        mPresenter.requestList(mBean.getId(), true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtils.unregister(this);
    }

    @OnClick({R.id.tv_qy, R.id.tv_gz, R.id.tv_topbar_left, R.id.tv_topbar_right})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_topbar_left:
                pop();
                break;
            case R.id.tv_topbar_right:
                start(EditLampFragment.newInstance(Const.TYPE_ADD, mBean, null));
                break;
            case R.id.tv_qy:
                clickTabDouble(false);
                showHideFragment(mFragmentList.get(0), mFragmentList.get(1));
                break;
            case R.id.tv_gz:
                clickTabDouble(true);
                showHideFragment(mFragmentList.get(1), mFragmentList.get(0));
                break;
        }
    }

    /**
     * @param isGz 是否选中双灯控制器
     */
    private void clickTabDouble(boolean isGz) {
        mTvTitles.get(isGz ? 1 : 0).setEnabled(false);
        mViews.get(isGz ? 1 : 0).setBackgroundResource(R.color.c_3862FF);
        mTvTitles.get(isGz ? 0 : 1).setEnabled(true);
        mViews.get(isGz ? 0 : 1).setBackgroundResource(android.R.color.transparent);
    }

    @Override
    public void showSingleList(List<LampSwitchBean> singleList) {
        SingleLampFragment fragment = (SingleLampFragment) mFragmentList.get(0);
        fragment.bindData(singleList);

        mHandler.obtainMessage(0).sendToTarget();
    }

    @Override
    public void showDoubleList(List<LampSwitchBean> nbDbChildSwitchList) {
        DoubleLampFragment fragment = (DoubleLampFragment) mFragmentList.get(1);
        fragment.bindData(nbDbChildSwitchList);
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        mHandler.obtainMessage(2).sendToTarget();
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        mHandler.obtainMessage(1).sendToTarget();
    }

}
